package main

import (
	"gin-project-01/gin-step04/gin14-gorm-base/models"
	routers2 "gin-project-01/gin-step04/gin14-gorm-base/routers"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"html/template"
)

// main
//
//		@Description:  20【Gin+GORM】Gin GORM的配置以及实现数据库表的增删改查(41分1秒)
// https://gorm.io/zh_CN/docs/index.html
//go get -u gorm.io/gorm
//go get -u gorm.io/driver/sqlite
//go get -u gorm.io/driver/mysql

func main() {
	r := gin.Default()
	//设置文件上传的最大值
	r.MaxMultipartMemory = 8 << 20 // 8MiB

	//设置自定义模板函数
	r.SetFuncMap(template.FuncMap{
		"UnixToTime": models.UnixToTime, //注册模板函数
		"GetDouble":  models.GetDouble,  //注册模板函数
	})
	r.LoadHTMLGlob("templates/**/*")
	r.Static("/static", "./static")

	//1-------基于cookie的存储引擎，secret123 参数是用于加密的密钥
	store := cookie.NewStore([]byte("secret123"))
	////store是前面创建的存储引擎，mysession是cookie的key
	r.Use(sessions.Sessions("mysession", store))

	//2------基于redis的存储引擎，secret123 参数是用于加密的密钥
	//store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret123"))
	////store是前面创建的存储引擎，mysession是cookie的key
	//r.Use(sessions.Sessions("mysession", store))

	//配置路由
	routers2.IndexRouters(r)
	routers2.UserRouters(r)
	routers2.FileRouters(r)
	routers2.CookieRouters(r)
	routers2.SessionRouters(r)
	r.Run(":8888")

}
